//
//  YYLHomeCommonView.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/31.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit

class YYLHomeCommonView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.clear
        
        repostsBtn = addChildBtn(imageName: "timeline_icon_retweet", title: "转发")
        commentsBtn = addChildBtn(imageName: "timeline_icon_comment", title: "评论")
        attitudesBtn = addChildBtn(imageName: "timeline_icon_unlike", title: "赞")
        //添加分隔线
        addSubview(firstLine)
        addSubview(secondLine)
        //各个控件布局
        repostsBtn?.snp.makeConstraints({ (make) in
            make.top.left.bottom.equalTo(self)
        })
        commentsBtn?.snp.makeConstraints({ (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(repostsBtn!)
            make.left.equalTo(repostsBtn!.snp.right)
        })
        attitudesBtn?.snp.makeConstraints({ (make) in
            make.top.bottom.right.equalTo(self)
            make.width.equalTo(repostsBtn!)
            make.left.equalTo(commentsBtn!.snp.right)
        })
        
        firstLine.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(commentsBtn!)
        }
        secondLine.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.right.equalTo(commentsBtn!)
        }
        
        
    }
    
    //模型接收数据
    var originalVM: YYLOriginalViewModel?{
        didSet{
            //评论 转发  点赞个数
            if (originalVM?.homeModel?.reposts_count ?? 0) > 0 {
                repostsBtn?.setTitle("\(originalVM?.homeModel?.reposts_count ?? 0)", for: .normal)
            }
            
            if (originalVM?.homeModel?.comments_count ?? 0) > 0 {
                commentsBtn?.setTitle("\(originalVM?.homeModel?.comments_count ?? 0)", for: .normal)
            }
            if (originalVM?.homeModel?.attitudes_count ?? 0) > 0 {
                attitudesBtn?.setTitle("\(originalVM?.homeModel?.attitudes_count ?? 0)", for: .normal)
            }
        }
    }
    
    
    //  转发
    var repostsBtn: UIButton?
    //  评论
    var commentsBtn: UIButton?
    //  赞
    var attitudesBtn: UIButton?
    //  竖分割线
    let firstLine: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    let secondLine: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
    //  添加按钮的方法
    func addChildBtn(imageName: String, title: String) -> UIButton {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background_highlighted"), for: .highlighted)
        btn.setImage(UIImage(named: imageName), for: .normal)
        btn.setTitle(title, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        self.addSubview(btn)
        
       return btn
    }
    
}
